/*
 * @Description: webpack开发配置
 * @Author: chenjiaxin
 * @Date: 2024-10-28 10:06:14
 * @LastEditTime: 2024-10-31 11:30:34
 */

const { merge: WebpackMerge } = require("webpack-merge");
const baseConfig = require("./webpack.config.base");
const { port, open, devtool, apiEnv, rootPath, staticDir, distDir } = require("./app.config");
const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');

const devConfig = {
  mode: "development",
  devtool,
  // 配置文件见：https://webpack.docschina.org/configuration/dev-server
  devServer: {
    static: {
      directory: rootPath(staticDir),
    }, // 指定静态文件的根目录
    compress: true, // 启用gzip压缩
    port, // 服务器端口
    open, // 自动打开浏览器
    hot: true, // 启动HMR
    client: {
      progress: true // 显示编译进度
    },
    proxy: {
      /*
      '/api': {
        target: "http://localhost:3000",
        pathRewrite: { '^/api': '' }, // 重写
        // secure: true, 需保证HTTPS证书有效，默认
        ws: true,  // 代理 websockets
        changeOrigin: true // 虚拟的站点需要更管origin
      } // 代理API请求
       */
    }
  },
  plugins: [new ReactRefreshWebpackPlugin()]
};

module.exports = (env) => {
  // 设置环境参数
  if(env.development) {
    process.env.NODE_ENV = "development";
  }
  return WebpackMerge(baseConfig(), devConfig)
}